﻿namespace Dissemination.MVC.Repositories
{
    using System.Linq;
    using System.Collections.Generic;
    using Dissemination.MVC.Models;
    using System;

    public class TargetAreaRepository : BaseRepository<TargetArea>
    {
        public TargetAreaRepository()
        {
            EntityName = "TargetAreas";
        }

        public override List<TargetArea> List()
        {
            return dataContext.TargetAreas.ToList();
        }

        public List<TargetArea> ListByProgramID(int programID)
        {
            var targetAreas = (from ta in dataContext.TargetAreas
                               .Include("Country")
                               select ta);
            return targetAreas.ToList();
       }

        public TargetArea GetLastTargetAreaByProgramAndCountry(int programID, int countryID)
        {
            var targetAreas = (from ta in dataContext.TargetAreas
                               where (ta.Program.ProgramID == programID && ta.Country.CountryID == countryID)
                               orderby ta.TargetAreaOrdinal descending
                               select ta);

            return targetAreas.FirstOrDefault();
        }

        public List<SurveyResult> GetSurveyResultsByUserID(Guid userID)
        {
            return dataContext.Reporters.Include("TargetArea.SurveyResults.Survey").FirstOrDefault(r => r.ReporterUserID == userID).TargetArea.SurveyResults.ToList();
        }

        public List<SurveyResult> GetSurveyResultsByTargetAreaID(int targetAreaID)
        {
            return dataContext.SurveyResults.Include("Survey").Where(sr => sr.TargetArea.TargetAreaID == targetAreaID).OrderBy(sr => sr.Survey.SurveyID).ToList();
        }

        public List<Seminar> GetSeminarsByUserID(Guid userID)
        {
            return dataContext.Reporters.Include("TargetArea.Country").Include("TargetArea.Seminars").FirstOrDefault(r => r.ReporterUserID == userID).TargetArea.Seminars.ToList();
        }

        public List<Seminar> GetSeminarsByTargetAreaID(int targetAreaID)
        {
            return dataContext.Seminars.Include("TargetArea.Country").Where(s => s.TargetArea.TargetAreaID == targetAreaID).ToList();
        }

        public List<Country> ListCountries()
        {
            return dataContext.Countries.ToList();
        }

        public override TargetArea Get(int id)
        {
            return dataContext.TargetAreas.Include("Country").Include("Program").Include("Reporters").FirstOrDefault(ta => ta.TargetAreaID == id);
        }

        //public override void Add(TargetArea obj)
        //{
        //    TargetArea last = GetLastTargetAreaByProgramAndCountry(obj.Program.ProgramID, obj.Country.CountryID);
        //    int ordinal = last == null ? 1 : last.TargetAreaOrdinal + 1;

        //    base.Add(obj);
        //}

        public void Add(TargetArea obj, int programID, int countryID)
        {
            Country country = dataContext.Countries.FirstOrDefault(c => c.CountryID == countryID);
            obj.Country = country;

            Program program = dataContext.Programs.FirstOrDefault(p => p.ProgramID == programID);
            obj.Program = program;

            TargetArea last = GetLastTargetAreaByProgramAndCountry(obj.Program.ProgramID, obj.Country.CountryID);
            int ordinal = last == null ? 1 : last.TargetAreaOrdinal + 1;
            obj.TargetAreaOrdinal = ordinal;

            base.Add(obj);
        }

        //public void Save(TargetArea targetArea, int programID, int countryID)
        //{
        //    Country country = dataContext.Countries.FirstOrDefault(c => c.CountryID == countryID);
        //    targetArea.Country = country;

        //    Program program = dataContext.Programs.FirstOrDefault(p => p.ProgramID == programID);
        //    targetArea.Program = program;

        //    base.Save();
        //}

        //public void Save(TargetArea targetArea, int countryID)
        //{
        //    Country country = dataContext.Countries.FirstOrDefault(c => c.CountryID == countryID);
        //    targetArea.Country = country;
        //    base.Save();
        //}

        public TargetArea GetByUserID(Guid userID)
        {
            return dataContext.Reporters.Include("TargetArea").FirstOrDefault(r => r.ReporterUserID == userID).TargetArea;
        }

        public Program GetProgramByProgramID(int programID)
        {
            return dataContext.Programs.Include("Surveys").FirstOrDefault(p => p.ProgramID == programID);
        }

    }
}
